#include<iostream>
#include<thread>
#include<unistd.h>
#include<functional>
#include<string>
#include<vector>
using namespace std;
template<class T>
using func_t=function<void(T)>;//返回值为void,参数为T
template<class T>
class Thread
{
    public:
    Thread(func_t<T> func,const string&name,T data)
    :_tid(0)
    ,_func(func)
    ,_threadname(name)
    ,isrunning(false)
    ,_data(data)
    {
    }
    static void*ThreadRoutine(void*args)
    {
        //(void)args;//仅仅是为了防止编译器有告警
        Thread*ts=static_cast<Thread*>(args);
        ts->_func(ts->_data);
        return nullptr;
    }
    bool Start()
    {
        int n=pthread_create(&_tid,nullptr,ThreadRoutine,this);
        if(n==0)
        {
        isrunning=true;
         return true;
        }
        return false;
    }
    bool Join()
    {
        if(!isrunning) return true;
        int n=pthread_join(_tid,nullptr);
        if(n==0)
        {
            isrunning=false;
            return true;
        }
        return false;

    }
    string GetThreadName()
    {
        return _threadname;
    }
    bool IsRunning()
    {
        return isrunning;
    }
    ~Thread()
    {}
    private:
    pthread_t _tid;
    string _threadname;
    bool isrunning;
    func_t<T> _func;
    T _data;
};

